<script setup>
import { reactive, ref, computed, onMounted } from 'vue'
import { useWardrobesStore } from '../stores/wardrobes'
import { ElMessage } from 'element-plus'
import { Money } from '@element-plus/icons-vue'

const store = useWardrobesStore()
const submitting = ref(false)
const isDesktop = ref(false)

const form = reactive({
  id: '',
  labor_fee: 0,
  parking_fee: 0,
  mileage_electricity_fee: 0,
  meals_fee: 0,
  platform_fee: 0,
  payout_installer: 0,
})

const totalCost = computed(() => 
  form.labor_fee + form.parking_fee + form.mileage_electricity_fee + form.meals_fee + form.platform_fee
)

const platformProfit = computed(() => 
  totalCost.value - form.payout_installer - form.platform_fee
)

async function submit() {
  if (!form.id) {
    ElMessage.error('请输入衣柜ID')
    return
  }
  
  if (form.payout_installer <= 0) {
    ElMessage.error('请设置合理的师傅报酬')
    return
  }
  
  submitting.value = true
  try {
    await store.settle(Number(form.id), {
      labor_fee: Number(form.labor_fee),
      parking_fee: Number(form.parking_fee),
      mileage_electricity_fee: Number(form.mileage_electricity_fee),
      meals_fee: Number(form.meals_fee),
      platform_fee: Number(form.platform_fee),
      payout_installer: Number(form.payout_installer),
    })
    
    ElMessage.success('结算信息提交成功！')
    Object.assign(form, {
      id: '', labor_fee: 0, parking_fee: 0, mileage_electricity_fee: 0,
      meals_fee: 0, platform_fee: 0, payout_installer: 0
    })
  } catch (error) {
    ElMessage.error('提交失败：' + error.message)
  } finally {
    submitting.value = false
  }
}

// 检测设备类型
function detectDevice() {
  const userAgent = navigator.userAgent.toLowerCase()
  const isMobile = /mobile|android|iphone|ipad|phone/i.test(userAgent)
  const isTablet = /tablet|ipad/i.test(userAgent)
  const screenWidth = window.innerWidth
  
  isDesktop.value = !isMobile && !isTablet && screenWidth > 1024
}

onMounted(() => {
  detectDevice()
  window.addEventListener('resize', detectDevice)
})
</script>

<template>
  <div class="settlement-container" :class="{ 'desktop': isDesktop }">
    <div class="page-header">
      <h1>💰 待结账 - 费用结算</h1>
      <p>记录安装完成后的各项费用，计算师傅报酬和平台收益</p>
    </div>

    <el-card class="form-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <span class="card-title">费用结算录入</span>
          <el-tag type="warning" size="small">待结账状态</el-tag>
        </div>
      </template>

      <el-form :model="form" label-width="150px" class="settlement-form">
        <el-form-item label="衣柜ID" required>
          <el-input 
            v-model="form.id" 
            placeholder="请输入衣柜ID"
            class="form-input"
          />
        </el-form-item>

        <el-divider content-position="left">
          <span class="divider-title">🔧 安装费用</span>
        </el-divider>

        <div class="form-grid">
          <el-form-item label="安装工时费">
            <el-input-number 
              v-model="form.labor_fee" 
              :min="0" 
              :precision="2"
              placeholder="0.00"
              class="form-input"
            />
            <span class="unit">元</span>
          </el-form-item>

          <el-form-item label="停车费用">
            <el-input-number 
              v-model="form.parking_fee" 
              :min="0" 
              :precision="2"
              placeholder="0.00"
              class="form-input"
            />
            <span class="unit">元</span>
          </el-form-item>
        </div>

        <el-divider content-position="left">
          <span class="divider-title">🚗 交通费用</span>
        </el-divider>

        <div class="form-grid">
          <el-form-item label="里程电费">
            <el-input-number 
              v-model="form.mileage_electricity_fee" 
              :min="0" 
              :precision="2"
              placeholder="0.00"
              class="form-input"
            />
            <span class="unit">元</span>
          </el-form-item>

          <el-form-item label="餐饮费用">
            <el-input-number 
              v-model="form.meals_fee" 
              :min="0" 
              :precision="2"
              placeholder="0.00"
              class="form-input"
            />
            <span class="unit">元</span>
          </el-form-item>
        </div>

        <el-divider content-position="left">
          <span class="divider-title">💼 平台费用</span>
        </el-divider>

        <div class="form-grid">
          <el-form-item label="平台使用费">
            <el-input-number 
              v-model="form.platform_fee" 
              :min="0" 
              :precision="2"
              placeholder="0.00"
              class="form-input"
            />
            <span class="unit">元</span>
          </el-form-item>

          <el-form-item label="支付安装师傅">
            <el-input-number 
              v-model="form.payout_installer" 
              :min="0" 
              :precision="2"
              placeholder="0.00"
              class="form-input"
            />
            <span class="unit">元</span>
          </el-form-item>
        </div>

        <el-divider content-position="left">
          <span class="divider-title">📊 费用统计</span>
        </el-divider>

        <div class="cost-summary">
          <div class="summary-item">
            <span class="summary-label">总费用：</span>
            <span class="summary-value total-cost">¥{{ totalCost.toFixed(2) }}</span>
          </div>
          <div class="summary-item">
            <span class="summary-label">师傅报酬：</span>
            <span class="summary-value installer-pay">¥{{ form.payout_installer.toFixed(2) }}</span>
          </div>
          <div class="summary-item">
            <span class="summary-label">平台收益：</span>
            <span class="summary-value platform-profit">¥{{ platformProfit.toFixed(2) }}</span>
          </div>
        </div>

        <el-form-item class="submit-section">
          <el-button 
            type="primary" 
            size="large" 
            @click="submit"
            :loading="submitting"
            class="submit-btn"
          >
            <el-icon><Money /></el-icon>
            提交结算信息
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="info-tips">
      <el-alert
        title="结算说明"
        type="info"
        :closable="false"
        show-icon
      >
        <template #default>
          <ul>
            <li>提交后衣柜状态将变更为"待存档"</li>
            <li>请仔细核对各项费用金额</li>
            <li>师傅报酬 = 安装工时费 + 停车费 + 里程电费 + 餐饮费</li>
            <li>平台收益 = 总费用 - 师傅报酬 - 平台使用费</li>
          </ul>
        </template>
      </el-alert>
    </div>
  </div>
</template>

<style scoped>
.settlement-container {
  max-width: 1000px;
  margin: 0 auto;
}

.settlement-container.desktop {
  max-width: none;
  margin: 0;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
  color: white;
}

.page-header h1 {
  margin: 0 0 10px 0;
  font-size: 2.5rem;
  font-weight: 700;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.page-header p {
  margin: 0;
  font-size: 1.1rem;
  opacity: 0.9;
}

.form-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: #303133;
}

.settlement-form {
  padding: 20px 0;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin: 20px 0;
}

.form-input {
  width: 100%;
}

.unit {
  margin-left: 8px;
  color: #909399;
  font-size: 14px;
}

.divider-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #606266;
}

.cost-summary {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 12px;
  padding: 20px;
  margin: 20px 0;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding: 10px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.summary-item:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.summary-label {
  font-weight: 600;
  color: #606266;
  font-size: 1.1rem;
}

.summary-value {
  font-weight: 700;
  font-size: 1.2rem;
}

.total-cost {
  color: #e6a23c;
}

.installer-pay {
  color: #67c23a;
}

.platform-profit {
  color: #409eff;
}

.submit-section {
  text-align: center;
  margin-top: 40px;
}

.submit-btn {
  padding: 15px 40px;
  font-size: 1.1rem;
  border-radius: 25px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.info-tips {
  margin-top: 20px;
}

.info-tips ul {
  margin: 10px 0 0 0;
  padding-left: 20px;
}

.info-tips li {
  margin-bottom: 5px;
  color: #606266;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header h1 {
    font-size: 2rem;
  }
  
  .form-grid {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .submit-btn {
    width: 100%;
    padding: 15px 20px;
  }
  
  .cost-summary {
    padding: 15px;
  }
  
  .summary-item {
    flex-direction: column;
    gap: 5px;
    text-align: center;
  }
}

@media (max-width: 480px) {
  .page-header h1 {
    font-size: 1.8rem;
  }
  
  .page-header p {
    font-size: 1rem;
  }
  
  .form-card {
    margin: 0 10px 20px 10px;
  }
  
  .settlement-form {
    padding: 15px 0;
  }
}
</style>


